package com.yaozheng.vocationaltraining.activity;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.ListView;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.adapter.NewsListAdapter;
import com.yaozheng.vocationaltraining.iview.ILoadMoreView;
import com.yaozheng.vocationaltraining.iview.ITokenBaseView;
import com.yaozheng.vocationaltraining.iview.ITrainingBulletinView;
import com.yaozheng.vocationaltraining.manage.ListViewLoadMoreManage;
import com.yaozheng.vocationaltraining.service.NewsListService;
import com.yaozheng.vocationaltraining.service.impl.NewsListServiceImpl;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

/**
 * 新闻列表
 * 
 * @author Administrator
 * 
 */
@EActivity(R.layout.activity_news_list)
public class NewsListActivity extends BaseActivity implements
		ITrainingBulletinView, SwipeRefreshLayout.OnRefreshListener,
		ILoadMoreView {
	@ViewById
	ListView listView;
	@Extra
	String title;
	@Bean(NewsListServiceImpl.class)
	NewsListService newsListService;
	/**
	 * 下拉刷新
	 */
	@ViewById
	SwipeRefreshLayout swipeRefreshLayout;

	int lastId;
	@Extra
	int cat;

	NewsListAdapter newsListAdapter;

	ListViewLoadMoreManage listViewLoadMoreManage;

	@AfterViews
	public void initView() {
		setHeadTitle(title);
		newsListService.init(this);

//		swipeRefreshLayout.postDelayed(new Runnable() {
//
//			@Override
//			public void run() {
//				swipeRefreshLayout.setRefreshing(true);
//				onRefresh();
//
//			}
//		}, 500);
		onRefresh();
		listViewLoadMoreManage = new ListViewLoadMoreManage(this, this,
				listView);
		swipeRefreshLayout.setOnRefreshListener(this);
	}

	/**
	 * 加载数据
	 */
	public void loadData() {
		newsListService.newsList(cat, lastId);
	}

	@UiThread
	@Override
	public void responseSuccess(JSONObject jsonObject) {
		cancelLoadDataProgressDialog();
		JSONArray newsesDataList = TypeUtils.getJsonArray(jsonObject, "data",
				"newses");
		if (newsesDataList != null) {
			if (newsListAdapter == null) {
				newsListAdapter = new NewsListAdapter(this, newsesDataList);
				listView.setAdapter(newsListAdapter);

			} else {
				if (lastId == 0) {
					newsListAdapter.getDataList().clear();
				}
				newsListAdapter.getDataList().addAll(newsesDataList);
				newsListAdapter.notifyDataSetChanged();
			}
		}
	}

	@UiThread
	@Override
	public void responseError(String error) {
		cancelLoadDataProgressDialog();
		alertMessage(error);
	}

	@Override
	public void cancelLoadDataProgressDialog() {
		swipeRefreshLayout.setRefreshing(false);
		listViewLoadMoreManage.loadMoreComplete();
	}

	@Override
	public void onRefresh() {
		lastId = 0;
		loadData();
	}

	@Override
	public void signUp(int position) {
		// TODO Auto-generated method stub

	}

	@Override
	public void signUpSuccess(JSONObject returnJsonObject) {
		// TODO Auto-generated method stub

	}

	@Override
	public void signUpError(String error) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean isResponseResult() {
		return !isFinishing();
	}

	@Override
	public void loadMoreData() {
		if (newsListAdapter != null) {
			int lastId = newsListAdapter.getLastId();
			if (lastId > 0) {
				this.lastId = lastId;
				loadData();
			}
		}
	}

}
